package com.lym.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lym.reggie.common.R;
import com.lym.reggie.entity.Category;
import com.lym.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //分页查询
    @GetMapping("/page")
    public R<Page> findPage(Integer page,Integer pageSize){
        log.info("page:{},pageSize:{}",page,pageSize);
        Page<Category> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);

        categoryService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
        // java.sql.SQLSyntaxErrorException: Unknown column 'is_deleted' in 'field list'
    }

    // 新增菜品分类
    @PostMapping
    public R<String> save(@RequestBody Category category){
        boolean b = categoryService.save(category);
        return R.success("新增分类成功");
    }

    //删除菜品分类
    @DeleteMapping
    public R<String> delete(@RequestParam("ids") long ids){

        categoryService.remove(ids);
        return R.success("删除成功");

    }

    //修改菜品分类
    @PutMapping
    public R<String> edit(@RequestBody Category category){
        boolean b = categoryService.updateById(category);
        if (b){
            return R.success("修改成功！");
        }else {
            return R.error("修改失败！");
        }
    }

    //获取菜品分类（添加菜品时需用）
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> categories = categoryService.list(queryWrapper);
        return R.success(categories);
    }




}
